package com.sengled.cloud.biz.union.mgt.api.config;

import com.sengled.cloud.biz.union.mgt.common.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
import org.springframework.session.web.http.DefaultCookieSerializer;
import redis.clients.jedis.JedisPoolConfig;


@Configuration
@EnableCaching//启用缓存，这个注解很重要；
@PropertySource("classpath:config/redis.properties")
public class RedisConfig {
    private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);

    @Autowired
    Environment environment;

    @Autowired
    SengledProperties sengledProperties;
    @Bean
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(environment.getProperty("redis.maxActive", Integer.class));
        poolConfig.setTestOnBorrow(environment.getProperty("redis.testOnBorrow", Boolean.class));
        poolConfig.setTestOnReturn(environment.getProperty("redis.testOnReturn", Boolean.class));
        poolConfig.setMaxIdle(environment.getProperty("redis.maxIdle", Integer.class));
        poolConfig.setMaxWaitMillis(environment.getProperty("redis.maxWait", Integer.class));
        return poolConfig;
    }
    @Bean
    public RedisConnectionFactory jedisConnectionFactory() {
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        poolConfig.setTestOnBorrow(environment.getProperty("redis.testOnBorrow", Boolean.class));
//        poolConfig.setTestOnReturn(environment.getProperty("redis.testOnReturn", Boolean.class));
//        poolConfig.setMaxIdle(environment.getProperty("redis.maxIdle", Integer.class));
//        poolConfig.setMaxWaitMillis(environment.getProperty("redis.maxWait", Integer.class));

        JedisConnectionFactory ob = new JedisConnectionFactory(jedisPoolConfig());
        ob.setUsePool(true);
        ob.setHostName(sengledProperties.getProperty("REDIS_ADDR"));
        ob.setPort(Integer.parseInt(sengledProperties.getProperty("REDIS_PORT")));
        ob.setPassword(sengledProperties.getProperty("REDIS_PWD"));
        ob.setTimeout(environment.getProperty("redis.timeout", Integer.class));
        ob.setDatabase(Integer.parseInt(sengledProperties.getProperty("REDIS_DB")));
//        ob.setDatabase(environment.getProperty("redis.session.db", Integer.class));

        return ob;
    }
    @Bean
    @Primary
    public RedisConnectionFactory jedisConnectionFactorySpringSession() {
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        poolConfig.setMaxTotal(environment.getProperty("redis.maxActive", Integer.class));
//        poolConfig.setTestOnBorrow(environment.getProperty("redis.testOnBorrow", Boolean.class));
//        poolConfig.setTestOnReturn(environment.getProperty("redis.testOnReturn", Boolean.class));
//        poolConfig.setMaxIdle(environment.getProperty("redis.maxIdle", Integer.class));
//        poolConfig.setMaxWaitMillis(environment.getProperty("redis.maxWait", Integer.class));

        JedisConnectionFactory ob = new JedisConnectionFactory(jedisPoolConfig());
        ob.setUsePool(true);
        ob.setHostName(sengledProperties.getProperty("REDIS_ADDR_1"));
        ob.setPort(Integer.parseInt(sengledProperties.getProperty("REDIS_PORT_1")));
        ob.setPassword(sengledProperties.getProperty("REDIS_PWD_1"));
        ob.setTimeout(environment.getProperty("redis.timeout", Integer.class));
        ob.setDatabase(Integer.parseInt(sengledProperties.getProperty("REDIS_DB_1")));
//        ob.setDatabase(environment.getProperty("redis.session.db", Integer.class));

        return ob;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate() {

        StringRedisTemplate redisTemplate = new StringRedisTemplate(jedisConnectionFactory());
        // 分布式锁,redisUtil需要注入StringRedisTemplate
//        RedisUtil.setRedisTemplate(redisTemplate);
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplateSrpingSession() {

        StringRedisTemplate redisTemplate = new StringRedisTemplate(jedisConnectionFactorySpringSession());
        // 分布式锁,redisUtil需要注入StringRedisTemplate
        RedisUtil.setRedisTemplate(redisTemplate);
        return redisTemplate;
    }

    /**
     * redis-session配置序列化工具,域名,路径等
     *
     * @return
     */
    @Bean
    public DefaultCookieSerializer defaultCookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("JSESSIONID");
        serializer.setCookiePath("/");
        serializer.setDomainName("sengled.com");
        //serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        return serializer;
    }

    /**
     * redis-session注入序列化工具
     *
     * @return
     */
    @Bean
    public RedisHttpSessionConfiguration redisHttpSessionConfiguration() {
        RedisHttpSessionConfiguration redisSession = new RedisHttpSessionConfiguration();
        redisSession.setMaxInactiveIntervalInSeconds(60);
        redisSession.setCookieSerializer(defaultCookieSerializer());
        return redisSession;
    }

    /**
     * 关闭redis自动配置：
     * To disable the automatic configuration add ConfigureRedisAction.NO_OP as a bean.
     *
     * @return
     */
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }


}
